﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ThapCam.Ulti
{
    public class RC4
    {
        private static int[] key = new int[] { 0x7d, 0x241, 0x27e, 0x159, 0x60, 0x2d4, 0x12e, 0x5f, 0x4a, 0x20a };
        private static byte[] mykey = new byte[0xff];
        private static byte[] sbox = new byte[0xff];

        private static byte[] Calculate(byte[] plaintxt, byte[] psw)
        {
            Initialize(psw);
            byte index = 0;
            byte num2 = 0;
            int length = plaintxt.Length;
            byte[] buffer = new byte[length];
            for (int i = 0; i < length; i++)
            {
                index = (byte)((index + 1) % 0xff);
                num2 = (byte)((num2 + sbox[index]) % 0xff);
                byte num5 = sbox[index];
                sbox[index] = sbox[num2];
                sbox[num2] = num5;
                byte num8 = (byte)((sbox[index] + sbox[num2]) % 0xff);
                int num4 = sbox[num8];
                buffer[i] = (byte)(plaintxt[i] ^ num4);
            }
            return buffer;
        }

        private static string CharsToHex(byte[] chars)
        {
            string str = string.Empty;
            int length = chars.Length;
            for (int i = 0; i < length; i++)
            {
                str = str + string.Format("{0:x2}", chars[i]);
            }
            return str;
        }

        public static string Decrypt(string src, string key)
        {
            byte[] plaintxt = HexToChars(src);
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            byte[] buffer3 = Calculate(plaintxt, bytes);
            return Encoding.UTF8.GetString(buffer3);
        }

        public static string Decrypt2(string srcStr)
        {
            string str2;
            try
            {
                string str = "";
                int length = srcStr.Length;
                int num2 = length / 4;
                for (int i = 0; i < length; i += 4)
                {
                    int num4 = int.Parse(srcStr.Substring(i, 4));
                    int index = (i / 4) % 10;
                    char ch = Convert.ToChar((int)((num4 - key[index]) - (num2 % 0x3e8)));
                    str = str + ch;
                }
                str2 = str;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str2;
        }

        public static string Encrypt(string src, string key)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(src);
            byte[] psw = Encoding.UTF8.GetBytes(key);
            return CharsToHex(Calculate(bytes, psw));
        }

        public static string Encrypt2(string srcStr)
        {
            string str = "";
            int length = srcStr.Length;
            for (int i = 0; i < length; i++)
            {
                char ch = srcStr[i];
                int num3 = ch;
                int index = i % 10;
                int num5 = (num3 + key[index]) + (length % 0x3e8);
                string str2 = num5.ToString().PadLeft(4, '0');
                str = str + str2;
            }
            return str;
        }

        private static byte[] HexToChars(string hex)
        {
            int length = hex.Length;
            byte[] buffer = new byte[length / 2];
            for (int i = (hex.Substring(0, 2) == "0x") ? 2 : 0; i < length; i += 2)
            {
                buffer[i / 2] = Convert.ToByte(hex.Substring(i, 2), 0x10);
            }
            return buffer;
        }

        private static void Initialize(byte[] pwd)
        {
            byte num4;
            byte index = 0;
            int length = pwd.Length;
            for (num4 = 0; num4 < 0xff; num4 = (byte)(num4 + 1))
            {
                mykey[num4] = pwd[num4 % length];
                sbox[num4] = num4;
            }
            for (num4 = 0; num4 < 0xff; num4 = (byte)(num4 + 1))
            {
                index = (byte)(((index + sbox[num4]) + mykey[num4]) % 0xff);
                byte num2 = sbox[num4];
                sbox[num4] = sbox[index];
                sbox[index] = num2;
            }
        }
    }
}
